package top.kuxiao.crmserver.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.kuxiao.crmserver.pojo.AccountManager;
import top.kuxiao.crmserver.pojo.IUser;
import top.kuxiao.crmserver.pojo.SaleChance;
import top.kuxiao.crmserver.pojo.SysUser;
import top.kuxiao.crmserver.service.IAccountManagerService;
import top.kuxiao.crmserver.service.ISaleChanceService;
import top.kuxiao.crmserver.utils.Auth;
import top.kuxiao.crmserver.utils.CookieUtil;
import top.kuxiao.crmserver.utils.ResultUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 销售机会 前端控制器
 * </p>
 *
 * @author kuxiao
 * @since 2022-01-12
 */
@Api(tags ="销售机会")
@RestController
@CrossOrigin
@RequestMapping("/sale-chance")
public class SaleChanceController {

    @Autowired
    ISaleChanceService saleChanceService;

    /**
     * 获取当前登录的信息
     */
    @Autowired
    CookieUtil cookieUtil;


    @Autowired
    IAccountManagerService accountManagerService;


    /**
     * 获取销售机会的全部信息
     */
    @Auth("SE,AM")
    @ApiOperation(notes = "就是个获取列表", value = "获取列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResultUtil testQuery(HttpServletRequest request) {
        List<SaleChance> saleChances = saleChanceService.queryAll();
        List<SaleChance> list = null;


        SysUser uer = cookieUtil.getUer(request);


        if ("AM".equals(uer.getPower())) {
            list = new ArrayList<>();
            AccountManager accountManager = (AccountManager) cookieUtil.getObject(request);
            for (SaleChance saleChance : saleChances) {
                if (uer.getName().equals(saleChance.getScCreator()) ||
                        accountManager.getId().equals(saleChance.getScAssignToId())) {
                    list.add(saleChance);

                }
            }
        }

        if (list != null) {
            return ResultUtil.resObj(list);
        }

        return ResultUtil.resObj(saleChances);
    }


    /**
     * 新增一个销售机会
     */
    @Auth("SE,AM")
    @ApiOperation(notes = "不需要创建人、创建时间、指派人、指派时间、状态", value = "新增销售机会")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultUtil resultUtil(SaleChance saleChance, HttpServletRequest request) {

        SysUser uer = cookieUtil.getUer(request);

        saleChance.setScCreator(uer.getName());

        saleChance.setScCreationTime(new Date());

        saleChance.setOppStateId(1001);

        if (saleChanceService.save(saleChance)) {
            return ResultUtil.resMsgOK("添加成功");
        }

        return ResultUtil.resMsgError("添加失败");
    }

    /**
     * 更新时销售机会的回显
     */
    @Auth("SE,AM")
    @ApiOperation(notes = "需要销售机会的id", value = "更新销售机会的回显")
    @ApiImplicitParam(name = "id", value = "销售机会的id")
    @RequestMapping(value = "toUpdate/{id}", method = RequestMethod.POST)
    public ResultUtil toUpdate(@PathVariable("id") Integer id) {
        SaleChance one = null;

        List<SaleChance> list = saleChanceService.queryAll();

        for (SaleChance chance : list) {
            if (chance.getScId().equals(id)) {
                one = chance;
            }
        }

        if (one != null) {

            return ResultUtil.resObj(one);
        }

        return ResultUtil.resMsgError("没找到");

    }

    /**
     * 更新销售机会
     */
    @Auth("SE,AM")
    @ApiOperation(value = "更新销售机会")
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public ResultUtil update(SaleChance saleChance) {

        System.out.println("-------------"+saleChance);


        if (saleChance.getOppStateId() == 1001) {
            if (saleChanceService.updateById(saleChance)) {
                return ResultUtil.resMsgOK("修改成功");
            } else {
                return ResultUtil.resMsgError("修改失败");
            }
        }
        return ResultUtil.resMsgError("不能更新");
    }

    /**
     * 删除销售机会
     */
    @Auth("SE,AM")
    @ApiOperation(notes = "需要销售机会的id", value = "删除销售机会")
    @ApiImplicitParam(name = "id", value = "销售机会的id")
    @RequestMapping(value = "del/{id}", method = RequestMethod.DELETE)
    public ResultUtil del(@PathVariable("id") Integer id, HttpServletRequest request) {
        SysUser uer = new CookieUtil().getUer(request);
        SaleChance saleChance = saleChanceService.getById(id);
        //是不是未指派状态
        if (saleChance.getOppStateId() == 1001) {
            //判断要删除的机会是不是当前用户创建的
            if (saleChance.getScCreator().equals(uer.getName())) {
                saleChanceService.removeById(id);
                return ResultUtil.resMsgOK("删除成功");
            } else {
                return ResultUtil.resMsgError("请用创建该机会的账号删除");
            }
        } else {
            return ResultUtil.resMsgError("该用户已经指派");
        }
    }


    /**
     * 将机会指派出去
     */
    @Auth("SE")
    @ApiOperation(notes = "需要销售机会的id", value = "指派销售机会")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "销售机会的id"),
            @ApiImplicitParam(name = "amId", value = "指派人的id")})
    @RequestMapping(value = "assign/{id}", method = RequestMethod.PUT)
    public ResultUtil assign(@PathVariable("id") Integer id, Integer amId) {
        SaleChance saleChance = saleChanceService.getById(id);

        System.out.println("修改前：" + saleChance);

        if (saleChance.getOppStateId() == 1001) {

            saleChance.setScAssignToId(amId);
            saleChance.setScAssignTime(new Date());
            saleChance.setOppStateId(1002);

            System.out.println("修改后：" + saleChance);

            if (saleChanceService.updateById(saleChance)) {
                return ResultUtil.resMsgOK("指派成功");
            }
            return ResultUtil.resMsgError("指派失败");

        } else {
            return ResultUtil.resMsgError("该机会已经指派");
        }
    }
}
